package com.central.comment.mq

import org.apache.rocketmq.common.message.MessageConst
import org.springframework.cloud.stream.annotation.EnableBinding
import org.springframework.cloud.stream.annotation.Input
import org.springframework.cloud.stream.annotation.Output
import org.springframework.messaging.Message
import org.springframework.messaging.MessageChannel
import org.springframework.messaging.MessageHeaders
import org.springframework.messaging.SubscribableChannel
import org.springframework.messaging.support.MessageBuilder
import org.springframework.util.MimeTypeUtils


const val integral = "comment_integral_service"

const val authenticate = "authenticate_service"

const val wxSubscribe = "wx_subscribe_service"

@EnableBinding(CommentSink::class)
open class CommentMqConfig

interface CommentSink {

    @Output(integral)
    fun integralProduct(): MessageChannel

    @Input(authenticate)
    fun authenticateInput(): SubscribableChannel

    @Output(wxSubscribe)
    fun wxSubscribeProduct(): MessageChannel
}


fun <T> T.toMessage(tag: String?): Message<T> =  MessageBuilder.withPayload(this)
    .setHeader(MessageConst.PROPERTY_TAGS, tag)
    .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
    .build()

/**
 * 发送积分服务
 */
fun <T> CommentSink.sendIntegralMes(msg: T, tag: String?) = this.integralProduct().send(msg.toMessage(tag))

/**
 * 发送微信消息订阅服务
 */
fun <T> CommentSink.sendWxSubscribe(msg: T, tag: String?) = this.wxSubscribeProduct().send(msg.toMessage(tag))

